package com.example.footballsystem.controller;

import com.example.footballsystem.entity.User;
import com.example.footballsystem.service.UserService;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/UserAccount")
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    UserService userService;
    //登录
    @GetMapping("/login")
    public ResponseEntity<Boolean> login(String account, String password, HttpServletRequest request, HttpServletResponse response){
        log.info(account+" "+password);
        String ip = request.getHeader("x-forwarded-for");
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        if(ip.equals("0:0:0:0:0:0:0:1")){
            ip="127.0.0.1";
        }
        System.out.println("IP地址："+ip);
        String agentStr = request.getHeader("user-agent");
        System.out.println(agentStr);
        UserAgent agent = UserAgent.parseUserAgentString(agentStr);
        //浏览器
        Browser browser = agent.getBrowser();
        System.out.println("类型："+browser.getBrowserType()+
                "\n名称："+browser.getName()+
                "\n厂商："+browser.getManufacturer()+
                "\n产品系列："+browser.getGroup()+
                "\n引擎："+browser.getRenderingEngine());

        //浏览器版本
        Version version = agent.getBrowserVersion();
        System.out.println("========================");
        System.out.println("主版本："+version.getMajorVersion()+
                "\n小版本："+version.getMinorVersion()+
                "\n完整版本："+version.getVersion());
        //操作系统
        System.out.println("========================");
        OperatingSystem os = agent.getOperatingSystem();
        System.out.println("名称："+os.getName()+
                "\n设备类型："+os.getDeviceType()+
                "\n产品系列："+os.getGroup()+
                "\n生成厂商："+os.getManufacturer());


        log.info("sdasads");
        if(userService.Logincheck(account,password,ip,browser.getName(),os.getName())){
            return ResponseEntity.ok().body(true);
        }
        return ResponseEntity.ok().body(false);
    }
    @GetMapping("/getAllUser")
    public ResponseEntity<List<User>> getAllUser(){
        log.info(userService.getAllUser().toString());
        return ResponseEntity.ok().body(userService.getAllUser());
    }
    //搜索
    @GetMapping("/searchUser")
    public ResponseEntity<List<User>> searchUser(String info){
        //log.info(info);
        return ResponseEntity.ok().body(userService.search(info));
    }
    //注册
    @PostMapping("/regist")
    public ResponseEntity<?> regist(@RequestBody User user){
        log.info("1231123123132");
        //log.info(user.getAccount()+" "+user.getPassword()+" "+user.getusername()+" "+user.getEmail()+" "+user.getPhone());
        if(userService.regist(user)) {
            return ResponseEntity.ok().body(true);
        }
        else
            return ResponseEntity.ok().body(false);
    }
    //删除
    @PostMapping("/deleteUser")
    public ResponseEntity<Boolean> deleteUsers(@RequestBody List<User> user){
        log.info(user.toString());
        for(int i=0;i<user.size();i++){
            userService.deleteUser(user.get(i).getAccount());
        }
        return ResponseEntity.ok().body(true);
    }
    @PostMapping("/getuser")
    public ResponseEntity<User> getUser(String account){
        log.info(account);
        log.info(userService.getUser(account).toString());
        return ResponseEntity.ok().body(userService.getUser(account));
    }
    @PostMapping("/changeUsername")
    public ResponseEntity<Boolean> changeUsername(String account,String username){
        return ResponseEntity.ok().body(userService.changeUserName(account,username));
    }
    @PostMapping("/changeUserBirthday")
    public ResponseEntity<Boolean> changeUserBirthday(String account,String birthday){
        log.info(birthday);
        return ResponseEntity.ok().body(userService.changeUserBirthday(account, birthday));
    }
    @PostMapping("/changeUserSex")
    public ResponseEntity<Boolean> changeUserSex(String account,String sex){
        return ResponseEntity.ok().body(userService.changeUserSex(account, sex));
    }
    @PostMapping("/changeUserEmail")
    public ResponseEntity<Boolean> changeUserEmail(String account,String email){
        return ResponseEntity.ok().body(userService.changeUserEmail(account,email));
    }
    @PostMapping("/changeUserPassword")
    public ResponseEntity<Boolean> changeUserPassword(String account,String password){
        log.info(account+" "+password);
        return ResponseEntity.ok().body(userService.changeUserPassword(account, password));
    }
    @GetMapping("/setScore")
    public ResponseEntity<Boolean> addScore(String account,int score){
        log.info(account+" "+score);
        return ResponseEntity.ok().body(userService.addScore(account,score));
    }
    @GetMapping("/getLoginCount")
    public ResponseEntity<List<Integer>> getLoginCount(String account){
        return ResponseEntity.ok().body(userService.getUserCount());
    }
    @GetMapping("/getRank")
    public ResponseEntity<List<User>> getRank(){
        log.info("1 2 3 "+userService.getRank().toString());
        return ResponseEntity.ok().body(userService.getRank());

    }
    @GetMapping("/changeUrl")
    public ResponseEntity<Boolean> changeUrl(String account,String url){
        return ResponseEntity.ok().body(userService.changeUrl(account,url));
    }
    @GetMapping("/count")
    public ResponseEntity<Integer> getCount(){
        return ResponseEntity.ok().body(userService.count());
    }
}
